package com.zjut.zs.confirm.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjut.an.util.module.PageModule;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZsConfirm entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zjut.zs.confirm.dao.ZsConfirm
 * @author MyEclipse Persistence Tools
 */

public class ZsConfirmDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(ZsConfirmDAO.class);
	// property constants
	public static final String ID = "id";
	public static final String US_ID = "usId";
	public static final String US_IDCARD = "usIdcard";
	public static final String US_NAME = "usName";
	public static final String XY_XK_IS = "xyXkIs";
	public static final String XY_YW = "xyYw";
	public static final String XY_WY = "xyWy";
	public static final String XY_SX = "xySx";
	public static final String XY_ZZ = "xyZz";
	public static final String XY_LS = "xyLs";
	public static final String XY_DL = "xyDl";
	public static final String XY_WL = "xyWl";
	public static final String XY_HX = "xyHx";
	public static final String XY_SW = "xySw";
	public static final String XY_JS = "xyJs";
	public static final String XY_ZXZH1 = "xyZxzh1";
	public static final String XY_ZXZH2 = "xyZxzh2";
	public static final String XY_ZXZH3 = "xyZxzh3";
	public static final String XY_ZXZH4 = "xyZxzh4";
	public static final String XY_ZXZH = "xyZxzh";
	public static final String XY_XK_ZZ = "xyXkZz";
	public static final String XY_XK_LS = "xyXkLs";
	public static final String XY_XK_DL = "xyXkDl";
	public static final String XY_XK_WL = "xyXkWl";
	public static final String XY_XK_HX = "xyXkHx";
	public static final String XY_XK_SW = "xyXkSw";
	public static final String XY_XK_JS = "xyXkJs";
	public static final String _CXK_IS = "CXkIs";
	public static final String _CYW = "CYw";
	public static final String _CWY = "CWy";
	public static final String _CSX = "CSx";
	public static final String _CZZ = "CZz";
	public static final String _CLS = "CLs";
	public static final String _CDL = "CDl";
	public static final String _CWL = "CWl";
	public static final String _CHX = "CHx";
	public static final String _CSW = "CSw";
	public static final String _CJS = "CJs";
	public static final String _CZXZH1 = "CZxzh1";
	public static final String _CZXZH2 = "CZxzh2";
	public static final String _CZXZH3 = "CZxzh3";
	public static final String _CZXZH4 = "CZxzh4";
	public static final String _CZXZH = "CZxzh";
	public static final String _CXK_ZZ = "CXkZz";
	public static final String _CXK_LS = "CXkLs";
	public static final String _CXK_DL = "CXkDl";
	public static final String _CXK_WL = "CXkWl";
	public static final String _CXK_HX = "CXkHx";
	public static final String _CXK_SW = "CXkSw";
	public static final String _CXK_JS = "CXkJs";

	protected void initDao() {
		// do nothing
	}

	public void sort(Criteria criteria, String sort, String order) {
		if (sort!=null && order!=null) {
			if (order.equals("asc")) {
				criteria.addOrder(Order.asc(sort));
			} else {
				criteria.addOrder(Order.desc(sort));
			}
		} else {
			criteria.addOrder(Order.desc(ID));			
		}
	}

	public void update(ZsConfirm transientInstance) {
		log.debug("updating ZsConfirm instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void save(ZsConfirm transientInstance) {
		log.debug("saving ZsConfirm instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZsConfirm persistentInstance) {
		log.debug("deleting ZsConfirm instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZsConfirm findById(java.lang.Integer id) {
		log.debug("getting ZsConfirm instance with id: " + id);
		try {
			ZsConfirm instance = (ZsConfirm) getHibernateTemplate().get(
					"com.zjut.zs.confirm.dao.ZsConfirm", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZsConfirm instance) {
		log.debug("finding ZsConfirm instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZsConfirm instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZsConfirm as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsId(Object usId) {
		return findByProperty(US_ID, usId);
	}

	public List findByUsIdcard(Object usIdcard) {
		return findByProperty(US_IDCARD, usIdcard);
	}

	public List findByUsName(Object usName) {
		return findByProperty(US_NAME, usName);
	}

	public List findByXyXkIs(Object xyXkIs) {
		return findByProperty(XY_XK_IS, xyXkIs);
	}

	public List findByXyYw(Object xyYw) {
		return findByProperty(XY_YW, xyYw);
	}

	public List findByXyWy(Object xyWy) {
		return findByProperty(XY_WY, xyWy);
	}

	public List findByXySx(Object xySx) {
		return findByProperty(XY_SX, xySx);
	}

	public List findByXyZz(Object xyZz) {
		return findByProperty(XY_ZZ, xyZz);
	}

	public List findByXyLs(Object xyLs) {
		return findByProperty(XY_LS, xyLs);
	}

	public List findByXyDl(Object xyDl) {
		return findByProperty(XY_DL, xyDl);
	}

	public List findByXyWl(Object xyWl) {
		return findByProperty(XY_WL, xyWl);
	}

	public List findByXyHx(Object xyHx) {
		return findByProperty(XY_HX, xyHx);
	}

	public List findByXySw(Object xySw) {
		return findByProperty(XY_SW, xySw);
	}

	public List findByXyJs(Object xyJs) {
		return findByProperty(XY_JS, xyJs);
	}

	public List findByXyZxzh1(Object xyZxzh1) {
		return findByProperty(XY_ZXZH1, xyZxzh1);
	}

	public List findByXyZxzh2(Object xyZxzh2) {
		return findByProperty(XY_ZXZH2, xyZxzh2);
	}

	public List findByXyZxzh3(Object xyZxzh3) {
		return findByProperty(XY_ZXZH3, xyZxzh3);
	}

	public List findByXyZxzh4(Object xyZxzh4) {
		return findByProperty(XY_ZXZH4, xyZxzh4);
	}

	public List findByXyZxzh(Object xyZxzh) {
		return findByProperty(XY_ZXZH, xyZxzh);
	}

	public List findByXyXkZz(Object xyXkZz) {
		return findByProperty(XY_XK_ZZ, xyXkZz);
	}

	public List findByXyXkLs(Object xyXkLs) {
		return findByProperty(XY_XK_LS, xyXkLs);
	}

	public List findByXyXkDl(Object xyXkDl) {
		return findByProperty(XY_XK_DL, xyXkDl);
	}

	public List findByXyXkWl(Object xyXkWl) {
		return findByProperty(XY_XK_WL, xyXkWl);
	}

	public List findByXyXkHx(Object xyXkHx) {
		return findByProperty(XY_XK_HX, xyXkHx);
	}

	public List findByXyXkSw(Object xyXkSw) {
		return findByProperty(XY_XK_SW, xyXkSw);
	}

	public List findByXyXkJs(Object xyXkJs) {
		return findByProperty(XY_XK_JS, xyXkJs);
	}

	public List findByCXkIs(Object CXkIs) {
		return findByProperty(_CXK_IS, CXkIs);
	}

	public List findByCYw(Object CYw) {
		return findByProperty(_CYW, CYw);
	}

	public List findByCWy(Object CWy) {
		return findByProperty(_CWY, CWy);
	}

	public List findByCSx(Object CSx) {
		return findByProperty(_CSX, CSx);
	}

	public List findByCZz(Object CZz) {
		return findByProperty(_CZZ, CZz);
	}

	public List findByCLs(Object CLs) {
		return findByProperty(_CLS, CLs);
	}

	public List findByCDl(Object CDl) {
		return findByProperty(_CDL, CDl);
	}

	public List findByCWl(Object CWl) {
		return findByProperty(_CWL, CWl);
	}

	public List findByCHx(Object CHx) {
		return findByProperty(_CHX, CHx);
	}

	public List findByCSw(Object CSw) {
		return findByProperty(_CSW, CSw);
	}

	public List findByCJs(Object CJs) {
		return findByProperty(_CJS, CJs);
	}

	public List findByCZxzh1(Object CZxzh1) {
		return findByProperty(_CZXZH1, CZxzh1);
	}

	public List findByCZxzh2(Object CZxzh2) {
		return findByProperty(_CZXZH2, CZxzh2);
	}

	public List findByCZxzh3(Object CZxzh3) {
		return findByProperty(_CZXZH3, CZxzh3);
	}

	public List findByCZxzh4(Object CZxzh4) {
		return findByProperty(_CZXZH4, CZxzh4);
	}

	public List findByCZxzh(Object CZxzh) {
		return findByProperty(_CZXZH, CZxzh);
	}

	public List findByCXkZz(Object CXkZz) {
		return findByProperty(_CXK_ZZ, CXkZz);
	}

	public List findByCXkLs(Object CXkLs) {
		return findByProperty(_CXK_LS, CXkLs);
	}

	public List findByCXkDl(Object CXkDl) {
		return findByProperty(_CXK_DL, CXkDl);
	}

	public List findByCXkWl(Object CXkWl) {
		return findByProperty(_CXK_WL, CXkWl);
	}

	public List findByCXkHx(Object CXkHx) {
		return findByProperty(_CXK_HX, CXkHx);
	}

	public List findByCXkSw(Object CXkSw) {
		return findByProperty(_CXK_SW, CXkSw);
	}

	public List findByCXkJs(Object CXkJs) {
		return findByProperty(_CXK_JS, CXkJs);
	}

	public List findAll() {
		log.debug("finding all ZsConfirm instances");
		try {
			String queryString = "from ZsConfirm";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZsConfirm merge(ZsConfirm detachedInstance) {
		log.debug("merging ZsConfirm instance");
		try {
			ZsConfirm result = (ZsConfirm) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZsConfirm instance) {
		log.debug("attaching dirty ZsConfirm instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZsConfirm instance) {
		log.debug("attaching clean ZsConfirm instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ZsConfirmDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ZsConfirmDAO) ctx.getBean("ZsConfirmDAO");
	}
	

	@SuppressWarnings("unchecked")
	public PageModule findByInstance(final int pageNumber ,final int pageSize, String sort, String order, final ZsConfirm obj){
		try {
			int startIndex = (pageNumber - 1) * pageSize;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			List list;
			Criteria criteria = session.createCriteria(ZsConfirm.class);
			

			if (!isEmpty(obj.getUsId())) {
				criteria.add(Restrictions.eq(US_ID, obj.getUsId()));
			}
			if (!isEmpty(obj.getUsIdcard())) {
				criteria.add(Restrictions.like(US_IDCARD, obj.getUsIdcard(),MatchMode.ANYWHERE));
			}
			if (!isEmpty(obj.getUsName())) {
				criteria.add(Restrictions.like(US_NAME, obj.getUsName(),MatchMode.ANYWHERE));
			}
			if (!isEmpty(obj.getCheckStatus())) {
				criteria.add(Restrictions.eq("checkStatus", obj.getCheckStatus()));
			}
			//补充
			
			this.sort(criteria, sort, order);
			
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			list = criteria.list();
			session.close();
			return new PageModule(pageNumber, pageSize, countByInstance(obj), list);
		} catch (RuntimeException re) {
			log.error("findByInstance failed", re);
			throw re;
		}
	}
	
	public int countByInstance(final ZsConfirm obj) {
		log.debug("finding the number of ZsConfirm instance");
		try {
			int count = 0;
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(ZsConfirm.class);
			

			if (!isEmpty(obj.getUsId())) {
				criteria.add(Restrictions.eq(US_ID, obj.getUsId()));
			}
			if (!isEmpty(obj.getUsIdcard())) {
				criteria.add(Restrictions.like(US_IDCARD, obj.getUsIdcard(),MatchMode.ANYWHERE));
			}
			if (!isEmpty(obj.getUsName())) {
				criteria.add(Restrictions.like(US_NAME, obj.getUsName(),MatchMode.ANYWHERE));
			}
			if (!isEmpty(obj.getCheckStatus())) {
				criteria.add(Restrictions.eq("checkStatus", obj.getCheckStatus()));
			}
			//补充
			
			
			count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
			session.close();
			return count;
		} catch (RuntimeException re) {
			log.error("find the number of ZsConfirm instance failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List findByHql(final int pageNumber ,final int pageSize, final String hql, final Object[] values){
		log.debug("finding ZsConfirm instance by hql");
	    return (List<ZsConfirm>) super.getHibernateTemplate().execute(new HibernateCallback(){
	        public Object doInHibernate(Session session) throws HibernateException, SQLException {
	            Query query=session.createQuery(hql);
	            if (values != null) {// 为hql语句传入参数
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}
	            return query.setFirstResult((pageNumber-1)*pageSize).setMaxResults(pageSize).list();
	        }
	    });
	}

	@SuppressWarnings("unchecked")
	public int findCountByHql(final String hql, final Object[] values) {
		log.debug("finding the number of ZsConfirm instance");
		try {
			return (Integer) super.getHibernateTemplate().execute(new HibernateCallback(){
		        public Object doInHibernate(Session session) throws HibernateException, SQLException {
		            Query query=session.createQuery(hql);
		            if (values != null) {// 为hql语句传入参数
						for (int i = 0; i < values.length; i++) {
							query.setParameter(i, values[i]);
						}
					}
					return ((Long)query.uniqueResult()).intValue();
		        }
		    });
		} catch (RuntimeException re) {
			log.error("find the number of ZsConfirm instance failed", re);
			throw re;
		}
	}
	
	private boolean isEmpty(String value) {
		if (value==null || value.trim().length()==0) {
			return true;
		}
		return false;
	}
}